﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;
using System.Linq;
using AvaloniaApplication1.Factory;
using AvaloniaApplication1.Models;
using AvaloniaApplication1.ViewModels.CardViewModel;
using CommunityToolkit.Mvvm.Input;
using Material.Icons;
using Microsoft.Extensions.Options;
using SukiUI.Dialogs;

namespace AvaloniaApplication1.ViewModels;

public partial class DevicePageViewModel:INotifyPropertyChanged
{

    private readonly ISukiDialogManager _dialogManager;
    public ISukiDialogManager DialogManager => _dialogManager;
     private readonly IViewModelFactory<DeviceCardModel, DeviceCardViewModel> _cardVmFactory;

    /// <summary>
    /// 所有设备
    /// </summary>
    private ObservableCollection<DeviceCardViewModel> _allDevices;
    /// <summary>
    /// 过滤后的设备
    /// </summary>
    private ObservableCollection<DeviceCardViewModel> _filteredDevices;
    private bool isupdateStatus;

    private IRelayCommand _filterCommand;
    public IRelayCommand FilterCommand => _filterCommand ??= new RelayCommand<string>(ToggleConnectionStatus);


    public ObservableCollection<DeviceCardViewModel> FilteredDevices
    {
        get => _filteredDevices;
        set
        {
            _filteredDevices = value;
            OnPropertyChanged();
        }
    }

    private string _connectionStatus="显示已连接";

    public string ConnectionStatus
    {
        get => _connectionStatus;
        set =>SetField(ref _connectionStatus, value);
    }


    public event PropertyChangedEventHandler? PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }
    public DevicePageViewModel(IViewModelFactory<DeviceCardModel, DeviceCardViewModel> cardVmFactory,
    IOptions<AppSettings> options,ISukiDialogManager dialogManager)
    {
        _dialogManager = dialogManager;
         // 初始化设备卡片列表
        _cardVmFactory = cardVmFactory;
        // 加载配置
        var devices = options.Value.Devices;
        if (devices.Count == 0){ Console.WriteLine("没有配置设备"); return;}
        _allDevices = new ObservableCollection<DeviceCardViewModel>(
            devices.Select(d => _cardVmFactory.Create(new DeviceCardModel(d.DeviceName, d.IsConnected, d.ConnectionType, d.ModbusParams)))
        );
        FilteredDevices = new ObservableCollection<DeviceCardViewModel>(_allDevices);

    }
    // 切换按钮连接状态文本
    private void ToggleConnectionStatus(string filterMode)
    {
        ConnectionStatus = filterMode.Equals("显示已连接") ? "显示全部" : "显示已连接";
        FilterByConnectionStatus(filterMode);
    }

    // 根据连接状态过滤设备
    private void FilterByConnectionStatus(string filterMode)
    {
        if (filterMode.Equals("显示全部"))
        {
            FilteredDevices = new ObservableCollection<DeviceCardViewModel>(_allDevices);
        }
        else
        {
            var filteredList = _allDevices
                .Where(vm => vm._deviceCardModel.IsConnected == filterMode.Equals("显示已连接"))
                .ToList();
            FilteredDevices = new ObservableCollection<DeviceCardViewModel>(filteredList);
        }
    }

}